home *** CD-ROM | disk | FTP | other *** search
- { Stream for memory-mapped files }
- type
- TMemoryFileStream = class(TFileStream)
- private
- FFileMapping : THandle;
- FFileBase : Pointer;
- public
- constructor Create(const FileName: String; Mode: Word);
- destructor Destroy; override;
- property Memory: Pointer read FFileBase;
- end;
- constructor TMemoryFileStream.Create(const FileName: String; Mode: Word);
- var
- aFlag : Integer;
- begin
- inherited Create(FileName, Mode);
- if (Mode and fmOpenReadWrite) <> 0 then
- aFlag := PAGE_READWRITE
- else
- aFlag := PAGE_READONLY;
- FFileMapping := CreateFileMapping(Handle, nil, aFlag, 0, 0, nil);
- if FFileMapping = 0 then
- raise Exception.Create('CreateFileMapping failed');
- if (Mode and fmOpenReadWrite) <> 0 then
- aFlag := FILE_MAP_WRITE
- else
- aFlag := FILE_MAP_READ;
- FFileBase := MapViewOfFile(FFileMapping, aFlag, 0, 0, 0);
- if FFileBase = nil then
- raise Exception.Create('MapViewOfFile failed');
- end;
-
- destructor TMemoryFileStream.Destroy;
- begin
- if FFileBase <> nil then
- UnmapViewOfFile(FFileBase);
- if FFileMapping <> 0 then
- CloseHandle(FFileMapping);
- inherited Destroy;
- end;